﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Comm;

public partial class Web_Admin_Admin_admin_add : AdminPageBase
{
    protected int id = 0;
    private BLL.Admin bll = new BLL.Admin();
    private BLL.Role rolebll = new BLL.Role();
    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.QueryInt("id");
        chk_IsPass.Enabled = base.CheckPopedomInfo(1001, 16);
        if (!IsPostBack)
        {
            loadType();
            loadData();
        }
    }

    /// <summary>
    /// 加载数据
    /// </summary>
    private void loadData()
    {
        if (id > 0)
        {
            but_save.Enabled = base.CheckPopedomInfo(1001, 4);
            Model.Admin model = bll.GetModel(id);
            if (model == null)
            {
                MessageBox.Show("该对象不存在");
            }

            this.Title = lit_title.Text = navigation1.Title = "管理员编辑";
            txt_UserName.Enabled = false;
            drpClass.SelectedValue = model.RoleID.Value.ToString();
            txt_UserName.Text = model.Username;
            hid_password.Value = model.Password;
            txt_Name.Text = model.Realname;
            txt_Email.Text = model.Email;
            chk_IsPass.Checked = model.IsPass;
        }
        else
        {
            base.CheckPopedomInfoRedirect(1001, 2);
            lit_password.Text = "&nbsp;<em>*</em>&nbsp;";
            lit_compassword.Text = "&nbsp;<em>*</em>&nbsp;";
        }
    }

    /// <summary>
    /// 加载类型
    /// </summary>
    private void loadType()
    {
        drpClass.DataSource = rolebll.DataTableToList(rolebll.GetList(0, "", "Ordinal asc").Tables[0]);
        drpClass.DataBind();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_save_Click(object sender, EventArgs e)
    {
        save();
    }

    /// <summary>
    /// 保存
    /// </summary>
    private void save()
    {
        Model.Admin model = myValidate();
        if (model == null)
        {
            return;
        }

        if (id > 0)
        {
            model.ID = id;
            if (string.IsNullOrEmpty(model.Password))
            {
                model.Password = hid_password.Value.Trim();
            }
            if (!bll.UpdateByID(model))
            {
                MessageBox.Show("修改失败");
            }
            base.AddLog(2, "管理员ID：" + model.ID);
        }
        else
        {
            List<Model.Admin> list = bll.GetModelList(@"UserName='" + model.Username + "'");
            if (list != null && list.Count > 0)
            {
                MessageBox.Show("该管理员已存在");
            }
            int i = bll.Add(model);
            if (i < 1)
            {
                MessageBox.Show("添加失败");
            }
            base.AddLog(1, "管理员ID：" + i);
        }

        Response.Redirect("admin_list.aspx");
    }

    /// <summary>
    /// 验证
    /// </summary>
    /// <returns></returns>
    private Model.Admin myValidate()
    {
        int roleid = int.Parse(drpClass.SelectedValue);
        string username = txt_UserName.Text.Trim();
        string password = txt_Password.Text.Trim();
        string name = txt_Name.Text.Trim();
        string email = txt_Email.Text.Trim();
        bool ispass = chk_IsPass.Checked;

        Model.Admin model = new Model.Admin();
        model.RoleID = roleid;
        model.Username = username;
        model.Password = password;
        model.Realname = name;
        model.Email = email;
        model.IsPass = ispass;
        model.Lastloginip = "";
        model.Lastlogintime = DateTime.Now;
        model.Logincount = 0;

        return model;
    }
}
